import tkinter as tk
from tkinter import ttk, messagebox
import webbrowser

class VideoParser:
    def __init__(self, root):
        self.root = root
        self.root.title("免费VIP视频解析")
        self.base_url = 'https://jx.xmflv.cc/?url='  # 解析服务地址提取为类属性
        
        # 窗口居中
        window_width = 500
        window_height = 150
        screen_width = self.root.winfo_screenwidth()
        screen_height = self.root.winfo_screenheight()
        center_x = int(screen_width/2 - window_width/2)
        center_y = int(screen_height/2 - window_height/2)
        self.root.geometry(f'{window_width}x{window_height}+{center_x}+{center_y}')
        
        # 主框架布局
        self.main_frame = ttk.Frame(self.root, padding=20)
        self.main_frame.grid(row=0, column=0, sticky="nsew")
        
        # 配置行列权重
        self.root.columnconfigure(0, weight=1)
        self.root.rowconfigure(0, weight=1)
        self.main_frame.columnconfigure(0, weight=1)
        self.main_frame.columnconfigure(1, weight=1)
        
        # 控件布局
        ttk.Label(self.main_frame, text="请输入视频链接：").grid(row=0, column=0, columnspan=2, sticky="w")
        
        self.input_var = tk.StringVar()
        self.entry = ttk.Entry(self.main_frame, textvariable=self.input_var, width=40)
        self.entry.grid(row=1, column=0, columnspan=2, sticky="ew", pady=(0, 10))
        
        self.parse_btn = ttk.Button(
            self.main_frame, 
            text="一键解析", 
            command=self.parse_video,
            style='TButton'
        )
        self.parse_btn.grid(row=2, column=0, columnspan=2, sticky="ew")
        
        # 焦点设置在输入框
        self.entry.focus_set()
        
    def parse_video(self):
        url = self.input_var.get().strip()
        if not url:
            messagebox.showerror("输入错误", "视频链接不能为空！")
            return
        if not url.startswith(('http://', 'https://')):
            messagebox.showerror("格式错误", "链接需以 http:// 或 https:// 开头")
            return
        
        parse_url = f'{self.base_url}{url}'
        try:
            webbrowser.open(parse_url)
        except webbrowser.Error as e:
            messagebox.showerror("浏览器错误", f"无法打开浏览器：{str(e)}")

def main():
    root = tk.Tk()
    # 设置UI风格
    style = ttk.Style()
    style.configure('TButton', font=('微软雅黑', 10))
    app = VideoParser(root)
    root.mainloop()

if __name__ == "__main__":
    main()